package jfdi;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.JsonReader;

import util.Mlog;

import java.io.IOException;

public class Node {

    static final String TAG = "node";
    
    protected NodeHost mHost;
    protected Paint mPaint;
    protected int mMinWidth;
    protected int mMinHeight;
    protected int mLeft;
    protected int mRight;
    protected int mTop;
    protected int mBottom;
    private Rect mDrawClip;
    protected int mBgColor = Color.LTGRAY;
    protected int mFgColor = Color.BLACK;
    
    public Node(NodeHost host) {
        mHost = host;
    }
    
    protected void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.BLACK);
        mDrawClip = new Rect();
    }
    
    public void setWidth(int w) {
        mMinWidth = w;
    }
    
    public void setHeight(int h) {
        mMinHeight = h;
    }
    
    public int getMinWidth() {
        return mMinWidth;
    }
    
    public int getMinHeight() {
        return mMinHeight;
    }
    
    public int getWidth() {
        return mRight - mLeft;
    }
    
    public int getHeight() {
        return mBottom - mTop;
    }
    
    public void layout(int l, int t, int r, int b) {
        mLeft = l;
        mRight = r;
        mTop = t;
        mBottom = b;
    }
    
    public void draw(Canvas canvas) {
        canvas.getClipBounds(mDrawClip);
        canvas.translate(mLeft, mTop);
        render(canvas);
        canvas.translate(-mLeft, -mTop);
        canvas.clipRect(mDrawClip);
    }

    public void render(Canvas canvas) {
    }

    // JSON serialization

    public void setAttribute(NodeHost host, String attr, JsonReader reader) throws IOException {
        if ("width".equals(attr)) {
            setWidth(reader.nextInt());
        } else if ("height".equals(attr)) {
            setHeight(reader.nextInt());
        }
    }

}
